package com.kayak.emailWorks.util;

import java.security.Security;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
//发送文本邮件
public class SendEmailText {

	public void sendAEmail(final String username, final String password,
			String title, String content, String toUser) {
		boolean isSSL = true;
		String host = "smtp.exmail.qq.com";
		int port = 465;
		String from = username;
		boolean isAuth = true;
		Properties props = new Properties();
		props.put("mail.smtp.ssl.enable", isSSL);
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.port", port);
		props.put("mail.smtp.auth", isAuth);
		Session session = Session.getInstance(props, new Authenticator() {
	@Override
	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(username, password);
			}
		});
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					toUser));
			message.setSubject(title);
			message.setText(content);		
			Transport tran = session.getTransport("smtp");
			tran.connect(host, username, password);
			tran.sendMessage(message, message.getAllRecipients());
			tran.close();
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		System.out.println("发送完毕！");
	}
	
	@SuppressWarnings("restriction")
	public static void main(String[] args) {
		String host = "smtp.263.net";
		String username="kayak-ios1@kayak.com.cn";
		String password="1qaz@WSX";
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());    
		final String SSL_FACTORY ="javax.net.ssl.SSLSocketFactory"; 
		String  port = "465";
		boolean isAuth = true;
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.port", port);
		props.put("mail.smtp.auth", isAuth);
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);    
        props.put("mail.smtp.socketFactory.fallback", "false");  
        props.put("mail.smtp.socketFactory.port", "465");
		Session session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("kayak-ios1@kayak.com.cn", "1qaz@WSX");
					}
				});
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(username));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					"1062413145@qq.com"));
			message.setSubject("【重要】梦姐姐提醒您提交出差时间啦");
			String content = EmailTempletMarket.SendMailTemplet("鲁鑫").toString();
			message.setText(content);				
			Transport tran = session.getTransport("smtp");
			tran.connect(host,username,password);
			Multipart mainPart = new MimeMultipart();
			MimeBodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setContent(content,"text/html; charset=utf-8");  
			mainPart.addBodyPart(messageBodyPart);
			message.setContent(mainPart);
			tran.sendMessage(message, message.getAllRecipients());
			tran.close();
			System.out.println("发送完毕！");
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}		
	}
}
